
Ext.define('FrontSuite.view.UserDialog', {
    extend:       'Ext.Window',  // defines the superclass. results in class inheritance.

    modal: true,
    closable: true,
    closeAction: 'hide',
    resizable: false,
    shadowOffset: 25,
    // msgArea: null,

    constructor: function (id, purpose, callback) {
        if (!purpose) purpose = 'login';
        if (id) this.id = id;
        else {
            id = 'user-'+purpose+'-dialog';
            this.id = id;
            this.defaultFocus = id + '-username';
        }
        //this.msgArea = Ext.create('Ext.Component', {
        //    id: this.id+'-msg'
        //});
        var dialog = this;

        var columnDefs = {
            username:  { column_name: 'username', column_label: 'Username',         help: 'help!!!' },
            password:  { column_name: 'password', column_label: 'Password',         not_null_ind: 'Y', display_option: 'password' },
            duration:  { column_name: 'duration', column_label: 'Duration (Hours)', column_type: 'integer',
                         valid_values: [1, 2, 3, 4, 5, 6, 9, 12, 15, 18, 21, 24],
                         default: 24 },
            email:        { column_name: 'email',        column_label: 'E-mail Address', not_null_ind: 'Y' },
            nick_name:    { column_name: 'nick_name',    column_label: 'Nick Name' },
            first_name:   { column_name: 'first_name',   column_label: 'First Name' },
            middle_name:  { column_name: 'middle_name',  column_label: 'Middle Name' },
            last_name:    { column_name: 'last_name',    column_label: 'Last Name' },
            birth_date:   { column_name: 'birth_date',   column_label: 'Birth Date', column_type: 'date' },
            cell_phone:   { column_name: 'cell_phone',   column_label: 'Cell Phone' },
            address1:     { column_name: 'address1',     column_label: 'Street Address' },
            city:         { column_name: 'city',         column_label: 'City' },
            state_code:   { column_name: 'state_code',   column_label: 'State Code' },
            postal_code:  { column_name: 'postal_code',  column_label: 'Postal Code' },
            country_code: { column_name: 'country_code', column_label: 'Country Code' }
        };

        var i, columns, extendedColumns, formFields, extendedFormFields;
        if (purpose === 'register') {
            this.title = 'Register';
            columns = ['email','password'];
            extendedColumns = ['username','nick_name','first_name','middle_name','last_name','birth_date','cell_phone','address1','city','state_code','postal_code','country_code'];
            formFields         = FrontSuite.app.createFormFieldsFromColumnDefs(columnDefs, columns, this.id);
            extendedFormFields = FrontSuite.app.createFormFieldsFromColumnDefs(columnDefs, extendedColumns, this.id);
            i = formFields.length;
            formFields[i] = {
                xtype: 'fieldset',
                title: 'Extra Fields',
                collapsible: true,
                collapsed: true,
                layout: 'anchor',
                defaults: { anchor: '100%' },
                items: extendedFormFields
            };
            buttons = [
                {
                    text: 'Register New User',
                    disabled: true,  // starts disabled
                    formBind: true,
                    handler: function(btn) {
                        btn.up('window').hide();
                        var form = btn.up('form').getForm();
                        if (form.isValid()) {
                            var userData = form.getValues();
                            var user_token = userData.email;
                            if (!user_token) user_token = userData.username;
                            if (!user_token) user_token = userData.cell_phone;
                            FrontSuite.util.ext.register(user_token, userData.password, userData, function (success, message) {
                                if (success == true) {
                                    if (!message) message = 'User '+user_token+' created successfully.';
                                    FrontSuite.util.ext.message('Information', message, 10000);
                                    form.reset();
                                }
                                else {
                                    FrontSuite.util.ext.error({message: message});
                                }
                            });
                        }
                    }
                },
                { text: 'Cancel', handler: function(btn) { btn.up('window').hide(); } }
            ];
        }
        else if (purpose === 'profile') {
            this.title = 'Edit User Profile';
            columns = ['email','password'];
            extendedColumns = ['username','nick_name','first_name','middle_name','last_name','birth_date','cell_phone','address1','city','state_code','postal_code','country_code'];
            formFields         = FrontSuite.app.createFormFieldsFromColumnDefs(columnDefs, columns, this.id);
            extendedFormFields = FrontSuite.app.createFormFieldsFromColumnDefs(columnDefs, extendedColumns, this.id);
            i = formFields.length;
            formFields[i] = {
                xtype: 'fieldset',
                title: 'Extra Fields',
                // collapsible: true,
                collapsed: false,
                layout: 'anchor',
                defaults: { anchor: '100%' },
                items: extendedFormFields
            };
            buttons = [
                {
                    text: 'Save',
                    disabled: true,  // starts disabled
                    formBind: true,
                    handler: function(btn) {
                        var form = btn.up('form').getForm();
                        if (form.isValid()) {
                        }
                    }
                },
                { text: 'Cancel', handler: function(btn) { btn.up('window').hide(); } }
            ];
        }
        else if (purpose == 'passchg') {
            this.title = 'Change Password';
            columns = ['email','password'];
            extendedColumns = ['username','nick_name','first_name','middle_name','last_name','birth_date','cell_phone','address1','city','state_code','postal_code','country_code'];
            formFields         = FrontSuite.app.createFormFieldsFromColumnDefs(columnDefs, columns, this.id);
            extendedFormFields = FrontSuite.app.createFormFieldsFromColumnDefs(columnDefs, extendedColumns, this.id);
            i = formFields.length;
            formFields[i] = {
                xtype: 'fieldset',
                title: 'Extra Fields',
                // collapsible: true,
                collapsed: false,
                layout: 'anchor',
                defaults: { anchor: '100%' },
                items: extendedFormFields
            };
            buttons = [
                {
                    text: 'Change Password',
                    disabled: true,  // starts disabled
                    formBind: true,
                    handler: function(btn) {
                        var form = btn.up('form').getForm();
                        if (form.isValid()) {
                        }
                    }
                },
                { text: 'Cancel', handler: function(btn) { btn.up('window').hide(); } }
            ];
        }
        else {  // 'login'
            this.title      = 'Login';
            columns    = ['email','password','duration'];
            formFields = FrontSuite.app.createFormFieldsFromColumnDefs(columnDefs, columns, this.id);
            buttons = [
                {
                    text: 'Login',
                    disabled: true,  // starts disabled
                    formBind: true,  // only enabled once the form is valid
                    handler: function(btn) {
                        var form = btn.up('form').getForm();
                        if (form.isValid()) {
                            var userData = form.getValues();
                            FrontSuite.util.ext.login(userData.email, userData.password, userData.duration, function (success, message) {
                                if (success == true) {
                                    if (callback) (callback)(true);
                                    form.reset();
                                    btn.up('window').hide();
                                }
                                else {
                                    FrontSuite.util.ext.error({message: message});
                                }
                            });
                            btn.up('window').hide();
                        }
                    }
                },
                { text: 'Cancel', handler: function(btn) { btn.up('window').hide(); } }
            ];
        }

        this.items = [{
            xtype: 'form',
            frame: true,
            resizable: false,
            closable: false,
            bodyStyle:'padding:5px 5px 0',
            width: 350,
            // height: 400,
            autoScroll: true,
            fieldDefaults: {
                msgTarget: 'side',
                labelWidth: 100
            },
            border: false,
            defaultType: 'textfield',
            defaults: {
                anchor: '100%'
            },
            items: formFields,
            buttons: buttons
        }];

        this.superclass.constructor.call(this, id);
        return(this);
    }
});

